package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Commodity;
import cn.wolfcode.trip.base.domain.Hotel;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ICommodityService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("commodity")
public class CommodityController {
    @Autowired
    private ICommodityService commodityService;

    @RequestMapping("list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        model.addAttribute("pageInfo",commodityService.query(qo));
        return "commodity/list";
    }

    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Commodity commodity, MultipartFile file){
        if (file != null && file.getSize() >0){
            String path = UploadUtil.upload(file, UploadUtil.PATH + "/upload");
            commodity.setCoverUrl(path);
        }
        commodityService.saveOrUpdate(commodity);
        return "redirect:/commodity/list.do";
    }

    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id){
        commodityService.deleteById(id);
        return new JsonResult();
    }
}
